﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Red.Core.Extensions;

namespace Red.Controls.ViewModels
{
    public class ObjectsPaletteItem : DataItem
    {
        #region Data

        private string _path;

        #endregion Data

        #region Properties

        public override IEnumerable<RedMenuDataItem> Menu
        {
            get
            {
                var menu = new List<RedMenuDataItem>();

                var nameOverride = $"Add {Value}";
                var commandWrapper = new CommandWrapper( "Add object", ObjectsPaletteCommands.AddObject, this );

                menu.Add( new RedMenuDataItem( commandWrapper, nameOverride, null, Image ) );
                return menu;
            }
        }

        public new ImageSource Image { get; }

        public override string Type
        {
            get { return "ObjectsPaletteItem"; }
        }

        public override string Path
        {
            get { return _path; }
        }

        public override string Value { get; set; }

        public override DataItemKind Kind
        {
            get { return DataItemKind.Simple; }
        }

        #endregion Properties

        #region C-tor

        public ObjectsPaletteItem( ObjectsPaletteItemInfo info, DataItem parent )
            : base( parent )
        {
            Value = info.Name;

            var bm = new BitmapImage();
            bm.BeginInit();
            bm.UriSource = new ResourceUri( info.ThumbnailPath );
            bm.EndInit();

            Image = bm;
            _path = info.Path;
        }

        #endregion C-tor

        public override IDataObject GetDraggedData()
        {
            return new DataObject( "RTTIValueHolder", Path );
        }

        public override int GetHashCode()
        {
            return Path.GetHashCode();
        }

        public override bool Equals( object obj )
        {
            var other = obj as ObjectsPaletteItem;
            if ( other == null )
                return false;

            return Path.Equals( other.Path );
        }
    }
}
